package com.luck.easyfoodbatch.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.luck.easyfoodbatch.mvp.model.Country;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmw
 * @ClassName: FindDB
 * @Description: 数据库查询类
 * @date 2014-12-2 下午1:42:15
 */
public class HitsDB {
    private static SQLiteDatabase database;
    private DBManager dBManager;

    public HitsDB(Context context) {
        dBManager = new DBManager(context);

    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 打开数据库
     * @author lmw
     */
    protected void openDataBase() {
        database = dBManager.openDatabase();
    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 关闭数据库
     * @author lmw
     */
    protected void closeDataBase() {
        if (null != database) {
            database.close();
        }
    }


    /**
     * 得到所有分类
     *
     * @return
     */
    public List<Hits> getByAllList() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from hits order by created_at desc", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                List<Hits> list = new ArrayList<Hits>(num);
                if (cur.moveToFirst()) {
                    do {
                        String hits_name = cur.getString(cur.getColumnIndex("hits_name"));
                        Hits hits = new Hits();
                        hits.setHits_name(hits_name);
                        list.add(hits);
                    } while (cur.moveToNext());
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.close();
            cur.close();
        }
        return null;
    }

    /**
     * 得到所有分类
     *
     * @return
     */
    public Hits getByLastList() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT *FROM hits ORDER BY \"_id\" asc LIMIT 1", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                Hits hits = new Hits();
                if (cur.moveToFirst()) {
                    do {
                        String hits_name = cur.getString(cur.getColumnIndex("hits_name"));
                        hits.setHits_name(hits_name);
                    } while (cur.moveToNext());
                }
                return hits;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.close();
            cur.close();
        }
        return null;
    }

    /**
     * @return
     */
    public boolean saveHitsList(String hitsName) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }

        if (!isHitsModel(hitsName)) {
            List<Hits> list = getByAllList();
            openDataBase();
            Hits hits = getByLastList();
            openDataBase();
            if (list.size() >= 10) {
                // 只保留10条历史记录
                String hits_name = hits.getHits_name();
                deleteHits(hits_name);
            }
            ContentValues values = new ContentValues();
            values.put("hits_name", hitsName);
            values.put("created_at", String.valueOf(System.currentTimeMillis()).substring(0, 10));
            database.insert("hits", null, values);
        }
        database.close();
        return true;
    }

    public boolean deleteHits(String hits_name) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        database.delete("hits", "hits_name = ?", new String[]{hits_name});
        //database.close();
        return true;
    }

    public boolean deleteDbHits(String hits_name) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        try {
            database.delete("hits", "hits_name = ?", new String[]{hits_name});
        } catch (Exception e) {
            e.printStackTrace();
            database.close();
            return false;
        }
        database.close();
        return true;
    }

    /**
     * 判断消息是否存在
     *
     * @return
     */
    public boolean isHitsModel(String hits_name) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        StringBuffer buffer = null;
        String sql = "SELECT * from hits where hits_name ='" + hits_name + "'";
        Cursor cur = database
                .rawQuery(sql, null);
        try {
            if (cur != null) {
                if (cur.moveToFirst()) {
                    do {
                        return true;
                    } while (cur.moveToNext());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //database.close();
            cur.close();
        }
        return false;
    }


    /**
     * @return void 返回类型
     * @Title: deleteDataBase
     * @Description: 删除数据库
     * @author lmw
     */
    public void deleteDataBase(Context context) {
        context.deleteDatabase("area.db");
    }
}
